function printSourceData() {
    console.log("Source Data:", scenetreedata)
}
function initSceneDAG(){
    window.SceneTreeData = scenetreedata
    var scenedag = {
        treeNodes: [],
        depth: 1
    }
    scenedag.treeNodes = getLeafNodes(window.SceneTreeData)
    for (let i = scenedag.treeNodes.length-1; i >= 0; i--){
        for(let j = 0; j < scenedag.treeNodes[i].length; j++){
            if(scenedag.treeNodes[i][j].graph){
                let sceneduration = initDAG(0, scenedag.treeNodes[i][j].graph)
                let parentKey = scenedag.treeNodes[i][j].parentKey
                let key = scenedag.treeNodes[i][j].key
                if(scenedag.treeNodes.length>1){
                    changeSceneNodeDuration(window.SceneTreeData, parentKey, key, sceneduration)
                }
            }
        }
    }

    if(scenedag.treeNodes.length>1){
        for (let i = 1; i<scenedag.treeNodes.length; i++) {
            for (let j = 0; j < scenedag.treeNodes[i].length; j++){
                if(scenedag.treeNodes[i][j].graph){
                    let parentKey = scenedag.treeNodes[i][j].parentKey
                    let key = scenedag.treeNodes[i][j].key
                    let SceneStartTime
                    for (let m = 0; m < scenedag.treeNodes[i-1].length; m++){
                        if(scenedag.treeNodes[i-1][m].key === parentKey){
                            if(scenedag.treeNodes[i-1][m].graph){
                                scenedag.treeNodes[i-1][m].graph.cells.forEach((n)=>{
                                    if(n.id === key){
                                        SceneStartTime = n.data.startTime
                                    }
                                })
                            }
                        }
                    }

                    renderSceneGraph(key, SceneStartTime, window.SceneTreeData)
                }
            }
        }
    }
    console.log("Process Result:",window.SceneTreeData)
}
/**按层获取json树的节点**/
function getLeafNodes(data){
    let arr = data
    let depth = 0;
    let treeNodes = []
    while (arr.length > 0) {
        let temp = [];
        for(let i = 0 ; i < arr.length ; i++){
            temp.push(arr[i]);
        }
        treeNodes.push(temp)
        arr = [];
        for(let i = 0 ; i < temp.length ; i++){
            if (temp[i].children){
                for(let j = 0 ; j < temp[i].children.length ; j++){
                    arr.push(temp[i].children[j]);
                }
            }
        }
        if(arr.length >= 0){
            depth++;
        }
    }
    return treeNodes;
}
/**修改graph中节点的duration**/
function changeSceneNodeDuration(data, parentKey, key, sceneduration){
    data.forEach((item)=>{
        if(item.key === parentKey){
            if(item.graph){
                item.graph.cells.forEach((cell)=>{
                    if(cell.shape === "animation-node" && cell.id === key){
                        cell.data.duration = sceneduration
                    }
                })
            }
        } else{
            if(item.children){
                changeSceneNodeDuration(item.children, parentKey, key, sceneduration)
            }
        }
    })
}
/**从场景父节点的graph中获取当前场景节点的starttime**/
function getParentStartTime(key, parentKey, data){
    let result
    data.forEach((item)=>{
        if(item.key === parentKey){
            if(item.graph){
                item.graph.cells.forEach((cell)=>{
                    if(cell.id === key){
                        result = cell.data.startTime
                    }
                })
            }
        }
        else {
            if(item.children){
                getParentStartTime(key, parentKey, item.children)
            }
        }
    })
    return result
}
/**根据startTime修改graph**/
function renderSceneGraph(key, startTime, data){
    data.forEach((s)=>{
        if(s.key === key){
            if(s.graph){
                initDAG(startTime,s.graph)
            }
        }
        else{
            if(s.children){
                renderSceneGraph(key, startTime, s.children)
            }
        }
    })
}

function initDAG(starttime, graph){
    var relation={
        startTime: 0,
        edges: [],
        nodes: [],
        zeroin: [],
        animationnodelist: [],
        result: []
    };
    if(starttime){
        relation.startTime = starttime
    }
    //从图中获取顶点序列和边序列
    graph.cells.forEach((item)=>{
        if (item.shape === "edge"){
            relation.edges.push(item);
        }
        else{
            relation.nodes.push(item);
        }
    })
    //寻找入度为0的顶点并加入【入度为0顶点】数组
    relation.zeroin = getZeroInNodes(relation.nodes, relation.edges);
    //计算动作顶点的开始时间和结束时间
    relation.zeroin.forEach((item)=>{
        item.data.startTime = relation.startTime + item.data.delay
        item.data.endTime = item.data.startTime + item.data.duration
        if (item.shape === "animation-node"){
            let before = item.attrs.text.text.split("\n")[0]
            item.attrs.text.text = before+"\n"+item.data.startTime+"s—"+item.data.endTime+"s"
        }
    })
    //获取动作顶点的后继节点
    while (relation.zeroin.length!==0){
        let successornodes = []
        let newzeroin = []
        relation.zeroin.forEach((item)=>{
            successornodes = getSuccessorNodes(item.id, relation.nodes, relation.edges);
            if(successornodes.length>0){
                successornodes.forEach((node)=>{
                    node.data.startTime = item.data.endTime + node.data.delay;
                    node.data.endTime = node.data.startTime + node.data.duration;
                    if (node.shape === "animation-node"){
                        let before = node.attrs.text.text.split("\n")[0]
                        node.attrs.text.text = before+"\n"+node.data.startTime+"s—"+node.data.endTime+"s"
                    }
                })
            }
            newzeroin.push(...successornodes)
        })
        relation.animationnodelist.push(...relation.zeroin)
        relation.zeroin = newzeroin
    }
    relation.nodes.forEach((node)=>{
        let sameidnodes = []
        relation.animationnodelist.forEach((item)=>{
            if(item.id === node.id){
                sameidnodes.push(item)
            }
        })
        if(sameidnodes.length>1){
            let maxStartTime = -999;
            let finalnodeindex
            sameidnodes.forEach((item,index)=>{
                if(item.data.startTime>maxStartTime){
                    maxStartTime = item.data.startTime
                    finalnodeindex = index
                }
            })
            relation.result.push(sameidnodes[finalnodeindex])
        } else{
            relation.result.push(...sameidnodes)
        }

    })
    let maxEndTime = -999
    relation.result.forEach((node)=>{
        if(node.data.endTime>maxEndTime){
            maxEndTime = node.data.endTime
        }
    })
    let durationTime = maxEndTime-relation.startTime
    return durationTime
}

/**寻找入度为0的顶点**/
function getZeroInNodes(nodes, edges){
    let sourcenodecount = 0;
    let zeroinnodes = [];
    nodes.forEach((node)=>{
        edges.forEach((edge)=>{
            if(edge.target.cell === node.id) {
                sourcenodecount ++;
            }
        })
        if (sourcenodecount === 0){
            zeroinnodes.push(node);
        }
        sourcenodecount = 0;
    })
    return zeroinnodes;
}
/**寻找当前顶点的后继节点**/
function getSuccessorNodes(nodeID, nodes, edges){
    let successornodes = []
    edges.forEach((edge)=>{
        if(edge.source.cell === nodeID){
            nodes.forEach((node)=>{
                if(node.id === edge.target.cell){
                    successornodes.push(node)
                }
            })
        }
    })
    return successornodes
}


